import { _decorator, Component, Sprite, tween, Vec3, Label, EventTarget} from 'cc';
import Util from './Util';
import { AnimationManager } from "./AnimationManager";
import { DirectorManager } from "./DirectorManager";
import { DiceController } from "./DiceController";
import { TableController } from "./TableController";

import { DeckCardManager } from "./DeckCardManager";
import { PlayerManager } from "./PlayerManager";
import { OutCardManager } from "./OutCardManager";

const { ccclass, property } = _decorator;

@ccclass('GameManager') // 用 @ccclass 声明类为 Cocos 组件
export class GameManager extends Component {
    // 通过 @property 让编辑器可以配置属性
    @property(DirectorManager) directorManage!: DirectorManager;
    @property(DiceController) diceControl!: DiceController;
    @property(TableController) tableControl: TableController | null = null;
    @property(DeckCardManager) deckCardManager!: DeckCardManager;
    @property(PlayerManager) playerManager!: PlayerManager;
    @property(OutCardManager) outCardManager!: OutCardManager;

    @property(Label) logText: Label | null = null;

	static instance: GameManager;
    static getDirector() {return GameManager.instance.directorManage;}
    static getDice() {return GameManager.instance.diceControl;}
    static getDeck() {return GameManager.instance.deckCardManager;}
    static getPlayer() {return GameManager.instance.playerManager;}
    static getOut() {return GameManager.instance.outCardManager;}

    private eventTarget: EventTarget = new EventTarget();

    onLoad() {
        GameManager.instance = this;
        this.setLog("GameManager onLoad");

        // Util.init();
    }

    addLog(str:string) {
        if(this.logText)
        {
            this.logText.string = this.logText.string + "\n" + str;
        }
    }

    setLog(str:string) {
        if(this.logText)
        {
            this.logText.string = str;
        }
    }

    getEvent() {
        return this.eventTarget;
    }

    start() {
        // 脚本启动时的逻辑
        GameManager.instance = this;
        console.log('GameManager started!');
    }

    onReset() {
        this.playerManager.onReset();
        this.outCardManager.onReset();
    }


    gameStart() {

        this.onReset();

        this.deckCardManager.initWholeDeck();

        this.gamePrepare();
        // this.scheduleOnce(this.onStartCard, 0.7);
    }


    gamePrepare() {
        //animation of uilayer
        AnimationManager.playGameStart();

        //up deck
        this.scheduleOnce(() => {
            AnimationManager.playUpwardDeck();
        }, 0.85);


        //roll dice
        this.scheduleOnce(() => {
            if(this.diceControl)
            {
                this.diceControl.randomNumber();
            }
        }, 1.4);

    }

    onStartCard() {
        
        this.deckCardManager.setStartPos();

        this.playerManager.onGameStart();
    }

    onCardAction() {

        const handCardManager = GameManager.getPlayer();
        for(let i = 0; i < 4; i++)
        {
            const player = handCardManager.getPlayer(i);
            if(player)
            {
                player.onAction(0);
            }
        }
    }

    onGameEnd()
    {
        DirectorManager.instance.setFirstViewport(5);
        this.playerManager.onGameEnd();
    }
}
